<!--
 * @Author: your name
 * @Date: 2021-07-09 17:19:14
 * @LastEditTime: 2021-07-12 21:07:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \day1\vue_shop\src\components\Home.vue
-->
<!--  -->
<template>
  <div class="com-container">
    <div class="com-chart">
      <el-container class="elcontainer">
        <!-- 头部区域 -->
        <el-header class="el-header">
          <div>
            <img src="~assets/img/ningweitou.jpg" alt="" />
            <span>后台管理系统</span>
          </div>
          <el-button type="info" @click="exit">退出</el-button>
        </el-header>
        <el-container>
          <!-- 侧边栏区域 -->
          <!-- 折叠时和不折叠时侧边栏宽度 -->
          <el-aside :width="isCollapse?'64px':'200px'">
            <!-- 侧边栏菜单区域 -->
            <!-- active-text-color="#ffd04b"激活的文本颜色 :unique-opened="true"实现每次只展开唯一的一项-->
             <!-- collapse是否折叠收起-->
             <!-- collapse-transition是否开启折叠动画 -->
             <div class="zhediebutton" @click="zhediebutton">
                |||
              </div>
            <el-menu router background-color="#333744" text-color="#fff" active-text-color="#409EFF" :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" :default-active="activePath">
              <!-- 一级菜单 -->
              <!-- id值都相同，所以绑定id -->
              <el-submenu :index="item.id+''" v-for="item in allData" :key="item.id">
                <!-- 一级菜单模板区 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i :class="iconObj[item.id]"></i>
                  <!-- 文本 -->
                  <span>{{item.authName}}</span>
                </template>
                <!-- 给二级菜单绑定点击事件，并且将地址的参数传送给函数 -->
                <el-menu-item :index="'/'+subitem.path" :key="subitem.id" v-for="subitem in item.children" @click="saveNavState('/' + subitem.path)">
                  <template slot="title">
                    <i class="el-icon-menu"></i>
                     <span>{{subitem.authName}}</span>
                    </template>
                </el-menu-item>
              </el-submenu>
            </el-menu>
          </el-aside>
          <!-- 右侧主体区域 -->
          <el-main><router-view></router-view></el-main>
        </el-container>
      </el-container>

    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单数据
      allData:null,
      iconObj:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-3702mima',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      isCollapse:false,
      //在data中定义一个数值，来存放地址
      //被激活的链接地址
      activePath:''
    }
  },
  //在页面被创建时，立即从sessionStorage中取出来，并且传递给activepath（参数地址）
  created(){
   this.getData(); 
   this.activePath=window.sessionStorage.getItem('activePath');
  },
  methods: {
    exit() {
      window.sessionStorage.clear()
      this.$router.push('/loginpage')
    },
   async getData(){
      const {data:res}=await this.$http.get('menus')
      if(res.meta.status!==200){
        return this.$message.error(this.meta.msg)
      }
      this.allData=res.data
      //console.log(res)
    },
    zhediebutton(){
      this.isCollapse=!this.isCollapse
    },
    //在方法中定义这个函数，并且将传过来的地址保存在sessionStorage中
    //保存链接的激活状态
    saveNavState(activePath) {
      // 将获取点击的地址保存在sessionStorage中
      window.sessionStorage.setItem('activePath', activePath)
      //最后在每一次完成点击时给activepath重新赋值(见第三点)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: rgb(21, 25, 29);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: aliceblue;
  padding-left: 0;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
  .zhediebutton{
    background-color: rgb(46, 49, 61);
    font-size: 10px;
    color:aliceblue;
    text-align: center;
  }
}
.elcontainer {
  height: 100%;
}
.el-main {
  background-color: rgb(236, 246, 252);
}
</style>
